package com.qf.bbapi.filter;

import com.qf.bbapi.config.RedisKeyConfig;
import com.qf.bbapi.util.JedisUtil;
import com.qf.bbapi.util.StringUtil;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@WebFilter("*.do")
@Component
public class TokenFilter implements Filter {
    /**
     * 令牌有效期续命，如果传递有token，则重置令牌有效期*/
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        String token = request.getIntHeader("token")+"";
        if(StringUtil.isNotEmpty(token)){
            JedisUtil.setExpire(RedisKeyConfig.USER_ID+token,RedisKeyConfig.LOGIN_TIME);
        }
        //放行
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
